package com.community.service.impl;

import com.community.annotation.SysLog;
import com.community.base.BaseService;
import com.community.common.Const;
import com.community.common.ServerResponse;
import com.community.pojo.Answer;
import com.community.pojo.AnswerAdopt;
import com.community.pojo.Question;
import com.community.service.IAnswerAdoptService;
import com.community.service.IIntegralService;
import com.community.util.LoginUtil;
import com.community.util.NumberUtil;
import com.community.util.PropertiesUtil;
import com.community.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 答案采纳记录服务层
 */
@Service("iAnswerAdoptService")
public class AnswerAdoptServiceImpl extends BaseService implements IAnswerAdoptService {

    @Autowired
    private IIntegralService iIntegralService;

    /**
     * 答案采纳
     * @param paramMap
     * @param request
     * @return
     */
    @Transactional(rollbackFor = { Exception.class })//事物控制
    @SysLog("答案采纳")
    public ServerResponse<String> add(Map paramMap, HttpServletRequest request){
        String adoptUser = StringUtil.valueOfString(paramMap.get("adopt_user"));
        if(!LoginUtil.isLogin(request,adoptUser)){//参数用户与登录用户不一致或未登录
            return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
        }
        int qid = NumberUtil.safeToNumber(paramMap.get("qid"));//问题ID
        Question question = questionMapper.selectByPrimaryKey(qid);
        //判断问题已有状态
        if(question.getStatus() != Const.QuestStatus.WQITHANDLE){//如果不是等待处理状态则返回错误
            return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
        }
        //判断提问用户是否与操作采纳用户一致
        if(!question.getProposeUser().equals(adoptUser)){
            return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
        }
        //更新问题状态
        question.setStatus(Const.QuestStatus.ANSWERED);//已解答状态
        if(questionMapper.updateByPrimaryKeySelective(question) <= 0){
            return ServerResponse.createByErrorMessage(Const.Msg.SAVEERROR);
        }
        //更新答案被采纳状态
        int aid = NumberUtil.safeToNumber(paramMap.get("aid"));//答案ID
        Answer answer = answerMapper.selectByPrimaryKey(aid);
        if(null == answer){
            throw new RuntimeException();
        }
        answer.setAdopt(Const.YesOrNo.YES);
        if(answerMapper.updateByPrimaryKeySelective(answer) <= 0){
            throw new RuntimeException();
        }
        //添加答案采纳记录
        AnswerAdopt answerAdopt = new AnswerAdopt();
        answerAdopt.setAnswerId(aid);//答案ID
        answerAdopt.setQid(qid);//问题ID
        answerAdopt.setAdoptUser(adoptUser);//采纳用户
        answerAdopt.setAdoptMoeny(question.getMoney());//金额
        if(answerAdoptMapper.insert(answerAdopt) <= 0){
            throw new RuntimeException();
        }
        if(question.getMoney() > 0){
            //增加被采纳用户积分
            ServerResponse response = iIntegralService.transaction(answer.getUsername(),PropertiesUtil.getProperty("CapitalAccount"),
                    question.getMoney(),
                    Const.TradingNotes.THREE,Const.TradingNotes.THREE);
            if(!response.isSuccess()){
                throw new RuntimeException();
            }
        }

        return ServerResponse.createBySuccessMessage(Const.Msg.SAVESUCCESS);
    }
}
